package com.htj.setting.web.controller;

import com.htj.setting.domain.User;
import com.htj.setting.service.UserService;
import com.htj.utils.MD5Util;
import com.htj.utils.PrintJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/User")
public class UserController {
    //服务层
    @Autowired
    UserService service;
    @RequestMapping(value = "/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response,String loginAct, String loginPwd){
        System.out.println("获得账号"+loginAct);
        System.out.println("密码"+loginPwd);
        System.out.println("开始登陆操作");
        String ip = request.getRemoteAddr();
        System.out.println("ip地址是=========="+ip);
        //密码进行MD5加密
        String pwd = MD5Util.getMD5(loginPwd);
        try {
            //登陆账号
            User user = service.login(loginAct,pwd,ip);
            //如果登陆成功,就把用户信息保存到session里面
            request.getSession().setAttribute("user",user);
            return PrintJson.printJsonFlag(true);
        }catch (Exception exception){
            //登陆失败
            //返回失败以及失败原因
            Map<String,Object> map = new HashMap<>();
            map.put("success",false);
            map.put("msg", exception.getMessage());
            System.out.println("异常信息"+exception.getMessage());
            return PrintJson.printJsonObj(map);
        }


    }
}
